home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Night Owl 6
/
Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso
/
039a
/
d3d.zip
/
CURVE3.C
< prev
next >
Wrap
Text File
|
1989-11-23
|
3KB
|
101 lines
/* CURVE3: B-spline curve fitting in three dimensions. The program
reads an input file and writes an output file, which are both
compatible with program D3D, so the following scheme is possible:
D3D --> input file --> CURVE3 --> output file --> D3D
The input file contains lines with point numbers i and three-
dimensional coordinates of the m points P[i], (i = 1, 2,..., m).
Any section beginning with the word 'Faces' in the file is
ignored. The program writes the three-dimensional coordinates of
the k points Q[j], (j = 1, 2, ..., k) in the output file (each
coordinate triplet preceded by a point number j), where k = (m-3)N-1.
The value of N is read from the keyboard. (N is the number of intervals
between to successive points P[i] and P[i+1].
Point Q[1] is an approximation of P[2];
Point Q[N+1] is an approximation of P[3];
Point Q[2N+1] is an approximation of P[4];
...
Point Q[k] is an approximation of P[m-1];
*/
#include <stdio.h>
#include <alloc.h>
#include <process.h>
main()
{
char infil[30], outfil[30];
int m=0, k, N, i, j, first=0;
float *x, *y, *z, X, Y, Z, t, xdum, ydum, zdum,
xA, xB, xC, xD, yA, yB, yC, yD, zA, zB, zC, zD,
a0, a1, a2, a3, b0, b1, b2, b3, c0, c1, c2, c3;
FILE *fpin, *fpout;
printf("Input file: "); scanf("%s", infil);
printf("Output file: "); scanf("%s", outfil);
printf("Enter N, the number of intervals between\n");
printf("two successive given points: "); scanf("%d", &N);
if((fpin = fopen(infil, "r")) == NULL) {
printf("File not available");
exit(1);
}
/* Scan the input file to see how much memory is required */
while(fscanf(fpin, "%*d %f %f %f", &xdum, &ydum, &zdum) > 0)
m++;
fclose(fpin);
x = (float *)malloc((m+1) * sizeof(float));
y = (float *)malloc((m+1) * sizeof(float));
z = (float *)malloc((m+1) * sizeof(float));
if(z == NULL) {
printf("Not enough memory");
exit(1);
}
fpin = fopen(infil, "r");
for(i=1; i<=m; i++)
fscanf(fpin, "%*d %f %f %f", x+i, y+i, z+i);
fclose(fpin);
fpout = fopen(outfil, "w");
for(i=2; i<m-1; i++) {
xA = x[i-1]; xB = x[i]; xC = x[i+1]; xD = x[i+2];
yA = y[i-1]; yB = y[i]; yC = y[i+1]; yD = y[i+2];
zA = z[i-1]; zB = z[i]; zC = z[i+1]; zD = z[i+2];
a3 = (-xA + 3.0*(xB-xC) + xD)/6.0;
a2 = (xA - 2.0*xB + xC)/2.0;
a1 = (xC - xA)/2.0;
a0 = (xA + 4.0*xB + xC)/6.0;
b3 = (-yA + 3.0*(yB-yC) + yD)/6.0;
b2 = (yA - 2.0*yB + yC)/2.0;
b1 = (yC - yA)/2.0;
b0 = (yA + 4.0*yB + yC)/6.0;
c3 = (-zA + 3.0*(zB-zC) + zD)/6.0;
c2 = (zA - 2.0*zB + zC)/2.0;
c1 = (zC - zA)/2.0;
c0 = (zA + 4.0*zB + zC)/6.0;
for(j=first; j<=N; j++) {
t = (float)j/(float)N;
X = ((a3*t + a2)*t + a1)*t + a0;
Y = ((b3*t + b2)*t + b1)*t + b0;
Z = ((c3*t + c2)*t + c1)*t + c0;
fprintf(fpout, "%d %f %f %f\n", (i-2)*N +j+1, X, Y, Z);
}
first = 1;
}
fprintf(fpout, "Faces:\n");
k = (m-3)*N + 1;
for(j=1; j<k; j++)
fprintf(fpout, "%d %d.\n", j, j+1);
fclose(fpout);
}